//
//  RootViewController.m
//  新月4
//
//  Created by penngshi on 15/12/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"
#import "Student.h"
@interface RootViewController ()<UITableViewDataSource , UITableViewDelegate>
@property (nonatomic , retain)UITableView *tableV;
@property (nonatomic , retain)NSMutableArray *stuArr;
@property (nonatomic , retain) NSMutableDictionary *stuDic;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    self.tableV = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
}
- (void)requestData

{
    NSString *file = [[NSBundle mainBundle]pathForResource:@"StudentInfo" ofType:@"plist"];
    NSMutableDictionary *dic  = [NSMutableDictionary dictionaryWithContentsOfFile:file];
    self.stuArr = [NSMutableArray array];
    for (NSString *key  in dic) {
        for (NSMutableDictionary *tempDic in [dic objectForKey:key]) {
            Student *stu = [[Student alloc]init];
            [stu setValuesForKeysWithDictionary:tempDic];
            [stu release];
             [self.stuDic setValue:stu forKey:key];
        }
       
    }
    self.stuArr = [NSMutableArray arrayWithArray:self.stuDic.allKeys];
    
    [self.stuArr sortedArrayUsingSelector:@selector(compare:)];
    
    
    
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableV setEditing:editing animated:animated];
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [self.stuArr objectAtIndex:indexPath.row];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (1 == self.stuArr.count) {
           
            [self.stuArr removeObjectAtIndex:indexPath.row];

        }
         else
         {
              [arr removeObjectAtIndex:indexPath.row];
         }
    }
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
   
}
- (NSArray *)tableVAtIndexes:(NSIndexSet *)indexes
{
    return self.stuArr;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.stuArr objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.stuArr objectAtIndex:section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];

    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cellStr";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (nil == cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    Student *stu = [self.stuArr objectAtIndex:indexPath.row];
    cell.textLabel.text = stu.name;
    cell.detailTextLabel.text = stu.phone;
    cell.imageView.image = stu.headerImage;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
